﻿//#define FULL_TRACE

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

using alib.Reflection;
using alib.Enumerable;
using alib.Debugging;

namespace agree.schema
{
	static class dbg
	{
		public static readonly Type T_RuntimeType;

		static dbg()
		{
			Debug.AutoFlush = true;
			T_RuntimeType = typeof(Type).UnderlyingSystemType.GetType();
		}

		public static bool IsRuntimeType(this Type t)
		{
			return t.GetType() == dbg.T_RuntimeType;
		}

		public static String obj_info<T>(T x)
		{
			var s = x as String;
			if (s != null)
				return s;
			if (x is bool)
				return ((bool)(Object)x) ? "true" : "false";
			var vt = typeof(T).IsValueType;
			if (vt ? x.Equals(default(T)) : (Object)x == default(Object))
				return vt ? "default(" + typeof(T)._Name() + ")" : "null";
			var t = x.GetType();
			String st = t._Name();
			if (t.IsArray)
				st = st.Replace("[]", "[" + (x as Array).Length + "]");

			st = "[" + st + "]";

			if (t.IsArray)
				st += " { " + ((IEnumerable<Object>)x).Select(z => z.ToString()).StringJoin(", ") + " }";

			return st;

		}
		public static String ex_info(Exception ex)
		{
			var s = ex.GetType()._Name() + "\r\n";
			if (ex.Message != null)
				s += "\t" + ex.Message + "\r\n";
			s += "\t" + ex.StackTrace + "\r\n";
			return s;
		}
		[Conditional("FULL_TRACE")]
		public static void write(String s)
		{
			Debug.Write(s);
		}
		[Conditional("FULL_TRACE")]
		public static void writeln(String s)
		{
			Debug.WriteLine(s);
		}
		[Conditional("FULL_TRACE")]
		public static void objinf<T>(T x)
		{
			Debug.WriteLine(obj_info(x));
		}
		[Conditional("FULL_TRACE")]
		public static void exinf(Exception ex)
		{
			Debug.WriteLine(ex_info(ex));
		}
	};
}